<?php
if (eregi("form.php",$_SERVER['SCRIPT_NAME'])) {
    Header("Location: ../index.php");
    die();
}


if (!defined('HTML_FORM_TEXT_SIZE')) { // default size of a text field
    define('HTML_FORM_TEXT_SIZE', 20);
}

if (!defined('HTML_FORM_MAX_FILE_SIZE')) { // maximal file size when uploading
    define('HTML_FORM_MAX_FILE_SIZE', 1048576); // 1 MB
}

if (!defined('HTML_FORM_PASSWD_SIZE')) { // default size of a password field
    define('HTML_FORM_PASSWD_SIZE', 8);
}

if (!defined('HTML_FORM_TEXTAREA_COLS')) { // default size for text area
    define('HTML_FORM_TEXTAREA_COLS', 40);
}

if (!defined('HTML_FORM_TEXTAREA_ROWS')) { // default size for text area
    define('HTML_FORM_TEXTAREA_ROWS', 8);
}

class HTMLForm {
// VARS: -
//
// METHODS:
// Begin (string, string [opt], string [opt], string [opt], string [opt], int [opt])
// End ()
// addText (string, string [opt], int [opt], int [opt], string [opt])
// addTextArea (string, string [opt], int [opt], int [opt], string [opt])
// addSubmit (string [opt], string [opt], string [opt])
// addReset (string [opt], string [opt])
// addSelect (string, array [key=>value], int [opt], int [opt], string [opt], bool [opt], string [opt], string [opt])
// addHidden (string, string [opt])
// addFile (string [opt], int [opt], int [opt])
// addMultipleFiles (string [opt], int [opt], int [opt], int[opt])
// addCheckBox (string, string [opt], string [opt])
// addPassword (stirng, string [opt], int [opt], string [opt])
// addRadio (string, string [opt], bool [opt], string [opt])
// isValid (string, string)


	// {{{ Begin()
    //
    // Create the begin of a HTML Form.
    //
    // IN: string [$action: target as an url-adress]
	//     string (optional) [$method: method]
	//     string (optional) [$name: form name]
	//     string (optional) [$target: target frame]
	//     int (optional) [$enctype: type of the form]
    // OUT: string [<form>-Tag]
    //
    function Begin($action, $method = "post", $name = "", $target = "", $enctype = 0)
    {
	    $html = "<form action='$action' method='$method'";
		if ($name !="") { $html .= " name='$name'"; }
		if ($target !="") { $html .= " target='$target'"; }
		switch ($enctype)
		{
		    case 1: $html .= " enctype='text/plain'"; break;
		    case 2: $html .= " enctype='multipart/form-data'"; break;
			case 3: $html .= " enctype='application/x-www-form-urlencoded'"; break;
		}
		$html .= ">\n";
		
		return $html;
    }
    // }}}


	// {{{ End()
    //
    // Return the error message.
    //
    // IN: -
    // OUT: string [end of the <form>-tag]
    //
	function End()
	{
		return "</form>\n";
	}
	// }}}


	// {{{ addText()
    //
    // Create a text field (<input>-tag).
    //
    // IN: string [$name: field name]
	//     string (optional) [$default: default value]
	//     int (optional) [$size: size of the field]
	//     int (optional) [$maxlength: maximal length of the field]
	//     string (optional) [$class: css style as class]
	//     int (optional) [$readonly: html attribute; 0 = no, 1 = yes]
    // OUT: string [<input>-tag]
    //
    function addText($name, $default = "", $size = HTML_FORM_TEXT_SIZE, $maxlength = "", $class = "", $readonly = 0)
    {
        $html = "<input name='$name' size='$size' value='$default'";
		    if ($maxlength !="" ) { $html .= " maxlength='$maxlength'"; }
			if ($class != "") { $html .= " class='$class'"; }
			if ($readonly == 1) { $html.= " readonly"; }
		$html .= ">\n";
		
        return $html;
    }
    // }}}


	// {{{ addTextArea()
    //
    // Create a text area (<textrea>-tag).
    //
    // IN: string [$name: field name]
	//     string (optional) [$default: default value]
	//     int (optional) [$size: size of the field]
	//     int (optional) [$maxlength: maximal length of the field]
	//     string (optional) [$class: css style as class]
    // OUT: string [<textarea>-tag]
    //
    function addTextArea($name, $default = "", $cols = HTML_FORM_TEXTAREA_COLS, $rows = HTML_FORM_TEXTAREA_ROWS,
	                     $maxlength = "", $class = "")
    {
        $html = "<textarea name='$name' cols='$cols' rows='$rows'";
		    if ($maxlength != "") { $html .= " maxlength='$maxlength'"; }
		    if ($class != "") { $html .= " class='$class'"; }
		$html .= ">";
        $html .= $default;
		$html .= "</textarea>\n";
		
        return $html;
    }
    // }}}


	// {{{ addSubmit()
    //
    // Create a submit button (<input>-tag).
    //
    // IN: string (optional) [$name: field name]
	//     string (optional) [$value: value]
	//     string (optional) [$class: css style as class]
    // OUT: string [<input>-tag]
    //
    function addSubmit($name = "", $value = "", $class = "submit")
    {
        $html = "<input type='submit'";
		    if ($name != "") { $html .= " name='$name'"; }
		$html .= " value='$value'";
		    if ($class != "") { $html .= " class='$class'"; }
	    $html .= ">\n";
		
        return $html;
    }
    // }}}


	// {{{ addReset()
    //
    // Create a reset button (<input>-tag).
    //
    // IN: string (optional) [$value: value]
	//     string (optional) [$class: css style as class]
    // OUT: string [<input>-tag]
    //
    function addReset($value = "", $class = "")
    {
	    $html = "<input type='reset' value='$value'";
            if ($class != "") { $html .= " class='$class'"; }
	    $html .= ">\n";
		
        return $html;
    }
    // }}}


	// {{{ addSelect()
    //
    // Create a secet tag (<select>-tag).
    //
    // IN: string [$name: field name]
	//     array ($key => $value [$entries: array with choices]
	//     int (optional) [$default: default (also as array possible!) value (as $key")]
	//     int (optional) [$size: size of the select window]
	//     string (optional) [$blank: ]
	//     bool (optional) [$multiple: whether multiple choices allowed]
	//     string (optional) [$attrib: attributes]
	//     string (optional) [$class: css style as class]
    // OUT: string [<select>-tag]
    //
    function addSelect($name, $entries, $default = "", $size = 1, $blank = "", $multiple = false, $attrib = "", $class = "")
    {
        if ($multiple && substr($name, -2) != "[]") { $name .= "[]"; }
        $html = "   <select name='$name'";
        if ($size) { $html .= " size='$size'"; }
        if ($multiple) { $html .= " multiple='multiple'"; }
        if ($attrib) { $html .= " $attrib"; }
		if ($class != "") { $html .= " class='$class'"; }
        $html .= ">\n";
        if ($blank) { $html .= "    <option value=\"\">$blank</option>\n"; }
        while (list($val, $text) = each($entries)) {
            $html .= '    <option ';
                if ($default) {
                    if ($multiple && is_array($default)) {
                        if ((is_string(key($default)) && $default[$val]) ||
                            (is_int(key($default)) && in_array($val, $default))) {
                            $html .= 'selected="selected" ';
                        }
                    } elseif ($default == $val) {
                        $html .= 'selected="selected" ';
                    }
                }
            $html .= "value=\"$val\">$text</option>\n";
        }
        $html .= "    </select>\n";
        
        return $html;
    }
    // }}}


	// {{{ addHidden()
    //
    // Create a hidden text (<input>-tag).
    //
    // IN: string [$name: field name]
	//     string (optional) [$value: value]
    // OUT: string [<input>-tag]
    //
    function addHidden($name, $value = "")
    {
        return "<input type='hidden' name='$name' value='$value'>\n";
    }
    // }}}


	// {{{ addFile()
    //
    // Create a file field (<input>-tag).
    //
    // IN: string (optional) [$name: field name]
	//     int (optional) [$maxsize: maximal size of the file]
	//     int (optional) [$size: size of the field]
    // OUT: string [<input>-tag]
    //
    function addFile($name = "userfile", $maxsize = HTML_FORM_MAX_FILE_SIZE, $size = HTML_FORM_TEXT_SIZE)
    {
        $html  = "    <input type='hidden' name='MAX_FILE_SIZE' value='$maxsize'>\n";
        $html .= "    <input type='file' name='$name' size='$size'>\n";
		
        return $html;
    }
    // }}}


	// {{{ addMultipleFiles()
    //
    // Create many file fields (<input>-tags).
    //
    // IN: string (optional) [$name: field name]
	//     int (optional) [$files: number of files]
	//     int (optional) [$maxsize: maximal size of the file]
	//     int (optional) [$size: size of the field]
    // OUT: string [<input>-tags]
    //
    function addMultipleFiles($name = "userfile[]", $files = 3, $maxsize = HTML_FORM_MAX_FILE_SIZE, $size = HTML_FORM_TEXT_SIZE)
    {
        $html = "    <input type='hidden' name='MAX_FILE_SIZE' value='$maxsize'>\n";
        for($i=0; $i < $files; $i++) {
            $html .= "    <input type='file' name='$name' size='$size'><br>\n";
        }
		
        return $html;
    }
    // }}}


	// {{{ addCheckbox()
    //
    // Create a checkbox (<input>-tag).
    //
    // IN: string [$name: field name]
	//     string (optional) [$default: default]
	//     string (optional) [$class: css style as class]
    // OUT: string [<input>-tag]
    //
    function addCheckbox($name, $default = "", $class = "checkbox")
    {
        $html = "<input type='checkbox' name='$name'";
            if ($default && $default != 'off') { $html .= " checked"; }
		    if ($class != "") { $html .= " class='$class'"; }
        $html .= ">\n";
        
        return $html;
    }
    // }}}


	// {{{ addPassword()
    //
    // Create a password field (<input>-tag).
    //
    // IN: string [$name: field name]
	//     string (optional) [$default: default]
	//     int (optional) [$size: size of the password field]
	//     string (optional) [$class: css style as class]
    // OUT: string [<input>-tag]
    //
    function addPassword($name, $default = "", $size = HTML_FORM_PASSWD_SIZE, $maxlength = 10, $class = "")
    {
        $html = "<input name='$name' type='password' value='$default' size='$size' maxlength='$maxlength'";
		    if ($class != "") { $html .= " class='$class'"; }
		$html.= ">\n";
		
		return $html;
    }
    // }}}


	// {{{ addRadio()
    //
    // Create a radio field (<input>-tag).
    //
    // IN: string [$name: field name]
	//     string (optional) [$value: value]
	//     bool (optional) [$default: whether the field is checked]
	//     string (optional) [$class: css style as class]
    // OUT: string [<input>-tag]
    //
    function addRadio($name, $value, $default = false, $class = "")
    {
	    $html = "<input type='radio' name='$name' value='$value'";
            if ($default != false) { $html .= " checked"; }
		    if ($class != "") { $html .= " class='$class'"; }
        $html.= ">\n";
		
		return $html;
    }
    // }}}


	// {{{ isValid()
    //
    // Check whether a var is valid.
    //
    // IN: string [$value: var]
	//     string [$expression: regular expression]
    // OUT: bool [TRUE if the var is valid, FALSE if not]
    //
    function isValid($var, $expression)
    {
	    switch ($expression)
		{
            case ":alnum:": $expression = "^[a-zA-Z0-9]+$"; break;
			case ":alpha:": $expression = "^[a-zA-Z]+$"; break;
			case ":digit:": $expression = "^[0-9]+$"; break;
			case ":lower:": $expression = "^[a-z]+$"; break;
			case ":allowernum:": $expression = "^[a-z0-9]+$"; break; // useful for passwords
			case ":upper:": $expression = "^[A-Z]+$"; break;
			case ":xdigit:": $expression = "^[a-fA-F0-9]+$"; break;
		}
		
        return ereg($expression, $var);
    }
    // }}}

}
?>